package com.kly.pay.service.impl;

import com.fqgj.common.api.Response;
import com.fqgj.exception.common.ApplicationException;
import com.google.api.services.androidpublisher.AndroidPublisher;
import com.google.api.services.androidpublisher.model.SubscriptionPurchasesAcknowledgeRequest;
import com.kly.enums.PayGatewayEnum;
import com.kly.pay.factory.GoogleApiService;
import com.kly.pay.factory.PayServiceFactory;
import com.kly.pay.service.BasicPayActionService;
import com.kly.req.RepayRequest;
import com.kly.req.RepayStatusRequest;
import com.kly.rsp.RepayResponse;
import com.kly.rsp.RepayStatusResponse;
import com.kly.service.pay.GooglePayService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.io.IOException;

/**
 * @author linqu
 */
@Slf4j
@Service("googlePayService")
public class GooglePayServiceImpl implements GooglePayService {

    @Resource
    PayServiceFactory payServiceFactory;


    @Override
    public RepayResponse repay(String payOrderNo, RepayRequest repayRequest) {
        final BasicPayActionService basicPayActionService = getBasicPayActionService(repayRequest.getPayGateway());
        return basicPayActionService.repay(payOrderNo, repayRequest);
    }

    @Override
    public RepayStatusResponse repayStatusQuery(RepayStatusRequest repayStatusRequest) {
        final BasicPayActionService basicPayActionService = getBasicPayActionService(repayStatusRequest.getPayGateway());
        return basicPayActionService.repayStatusQuery(repayStatusRequest);
    }

    @Override
    public String queryExtentInfo(RepayRequest repayRequest) {
        final BasicPayActionService basicPayActionService = getBasicPayActionService(repayRequest.getPayGateway());
        return basicPayActionService.queryExtentInfo(repayRequest);
    }

    /**
     * 用于订阅类商品操作：取消订阅
     * @param packageName 软件包名称
     * @param productId 订阅的 ID
     * @param purchaseToken 订阅时向用户设备提供的令牌
     * @return
     */
    @Override
    public Response<Boolean> cancelSubscription(String packageName, String productId, String purchaseToken) {

        try {
            final AndroidPublisher.Purchases.Subscriptions.Cancel cancel = getGoogleApiService(packageName).getSubscriptions().cancel(packageName, productId, purchaseToken);
            cancel.execute();
            log.info("AndroidPublisher.Purchases.Subscriptions.cancelSubscription success, packageName:{}, productId:{}, purchaseToken:{}", packageName, productId, purchaseToken);
            return Response.ok(Boolean.TRUE);
        } catch (IOException ex) {
            log.error("AndroidPublisher.Purchases.Subscriptions.cancelSubscription failed, packageName:{}, productId:{}, purchaseToken:{}", packageName, productId, purchaseToken, ex);
            throw new ApplicationException("AndroidPublisher.Purchases.Subscriptions.cancelSubscription failed");
        }
    }

    /**
     * 用于订阅类商品操作：订阅确认
     * @param packageName 软件包名称
     * @param productId 订阅的 ID
     * @param purchaseToken 订阅时向用户设备提供的令牌
     * @return
     */
    @Override
    public Response<Boolean> acknowledgeSubscription(String packageName, String productId, String purchaseToken) {
        try{
            SubscriptionPurchasesAcknowledgeRequest content = new SubscriptionPurchasesAcknowledgeRequest();
            final AndroidPublisher.Purchases.Subscriptions.Acknowledge acknowledge
                    = getGoogleApiService(packageName).getSubscriptions().acknowledge(packageName, productId, purchaseToken, content);
            acknowledge.execute();
            log.info("AndroidPublisher.Purchases.Subscriptions.acknowledge success, packageName:{}, productId:{}, purchaseToken:{}", packageName, productId, purchaseToken);
            return Response.ok(Boolean.TRUE);
        } catch (Exception ex) {
            log.error("AndroidPublisher.Purchases.Subscriptions.acknowledge failed, packageName:{}, productId:{}, purchaseToken:{}", packageName, productId, purchaseToken, ex);
            throw new ApplicationException("AndroidPublisher.Purchases.Subscriptions.acknowledge failed");
        }
    }

    private BasicPayActionService getBasicPayActionService(String payTypeStr) {
        final PayGatewayEnum payType = PayGatewayEnum.getEnumByCode(payTypeStr);
        return payServiceFactory.buildPayProcess(payType);
    }


    private GoogleApiService getGoogleApiService(String packageName) {
        return payServiceFactory.buildGooglePayChannel(packageName);
    }
}
